package ssm.pojo;
//购物车列

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;

public class Cart {
    private Double countPrice = 0.0;//购物车总金额
    private Map<String, CartItem> map = new LinkedHashMap<>();//多个单列购物车

    public Collection<CartItem> getCartItem() {
        return map.values();
    }


    public Cart() {
    }

    public Cart(Map<String, CartItem> map) {

        this.map = map;
    }

    public Double getCountPrice() {
        return countPrice;
    }

    public void setCountPrice(Double countPrice) {
        this.countPrice = countPrice;
    }

    public Map<String, CartItem> getMap() {
        return map;
    }

    public void setMap(Map<String, CartItem> map) {
        this.map = map;
    }

    @Override
    public String toString() {
        return "Cart{" +
                "countPrice=" + countPrice +
                ", map=" + map +
                '}';
    }

    //添加购物车
    public void addCartItem(CartItem cartItem) {
        String pid = cartItem.getProduct().getPid();
        //判断集合中是否有此商品
        if (map.containsKey(pid)) {
            int i = map.get(pid).getCount() + cartItem.getCount();
            map.get(pid).setCount(i);
        } else {
            map.put(pid, cartItem);
        }
        countPrice += cartItem.getPrice();
    }

    //删除购物车
    public void deleteCartItem(String pid) {
        CartItem cartItem = map.remove(pid);
        countPrice -= cartItem.getPrice();
    }

    //清空购物车
    public void cleanCartItem() {
        map.clear();
        countPrice = 0.0;
    }

}
